import logging
import logging.handlers
from datetime import datetime

# 1.创建日志器
logger = logging.getLogger("sewellhe")
# 设置日志级别
logger.setLevel(level=logging.DEBUG)

# 获取当前日期，格式为 YYYY-MM-DD
current_date = datetime.now().strftime('%Y-%m-%d')

# 2.创建处理器
# 控制台处理器
handler_stream = logging.StreamHandler()
# 文件处理器
handler_file_info = logging.handlers.TimedRotatingFileHandler(filename=f'info-{current_date}.log', when='midnight', backupCount=7, encoding='utf-8')
handler_file_warning = logging.handlers.TimedRotatingFileHandler(filename=f'warning-{current_date}.log', when='midnight', backupCount=7, encoding='utf-8')
handler_file_error = logging.handlers.TimedRotatingFileHandler(filename=f'error-{current_date}.log', when='midnight', backupCount=7, encoding='utf-8')

# 3.设置格式器
fmt = '[%(asctime)s] [%(filename)-15s] [函数名:%(funcName)-20s] [行号:%(lineno)-3d] [%(levelname)s]:%(message)s'
formatter = logging.Formatter(fmt=fmt)

# 4.关联
# 设置每个处理器的格式和日志级别
handler_stream.setFormatter(formatter)
handler_stream.setLevel(logging.INFO)

handler_file_info.setFormatter(formatter)
handler_file_info.setLevel(logging.INFO)

handler_file_warning.setFormatter(formatter)
handler_file_warning.setLevel(logging.INFO)

handler_file_error.setFormatter(formatter)
handler_file_error.setLevel(logging.INFO)

# 将处理器添加到日志器
logger.addHandler(handler_stream)
logger.addHandler(handler_file_info)
logger.addHandler(handler_file_warning)
logger.addHandler(handler_file_error)

# while 1:
#     logger.info('test...........')